﻿//==============================================================================
// File: ModuleBase.cs
// Created: 2010-06-07
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Globalization;
using WpfRcp.Core.Globals;
using WpfRcp.Core.Services;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Presentation.Events;
using Microsoft.Practices.Composite.Regions;

namespace WpfRcp.Core.Common
{
    public abstract class ModuleBase : IModule
    {
        private readonly IContainerFacade m_Container;

        protected ModuleBase(
            IContainerFacade container)
        {
            if (container == null)
                throw new ArgumentNullException("container");

            m_Container = container;

            var eventAggregator = Container.Resolve<IEventAggregator>();
            eventAggregator.GetEvent<LanguageChangedEvent>().Subscribe(OnLanguageChanged, ThreadOption.UIThread, true);
            eventAggregator.GetEvent<ApplicationStartedEvent>().Subscribe(OnApplicationStartup, ThreadOption.UIThread, true);
        }

        protected virtual bool LoadModule()
        {
            return true;
        }

        protected virtual void PreInit()
        {
            
        }

        protected virtual void PostInit()
        {

        }

        protected virtual void RegisterServices()
        {
        }

        protected virtual void RegisterViews()
        {
        }

        protected virtual void RegisterMenus()
        {
        }

        protected virtual ModuleInfoItem GetModuleInfo()
        {
            return null;
        }

        protected virtual void OnLanguageChanged(CultureInfo cultureInfo)
        {
            SetupModuleInfo();
        }

        protected virtual void OnApplicationStartup(object param)
        {
            
        }

        protected IContainerFacade Container
        {
            get { return m_Container; }
        }

        private void SetupModuleInfo()
        {
            ModuleInfoItem infoItem = GetModuleInfo();
            if (infoItem != null)
            {
                var regionManager = Container.Resolve<IRegionManager>();
                regionManager.RegisterViewWithRegion(RegionNames.ModulesInfoRegion, () => infoItem);
            }
        }

        #region IModule Implementation

        void IModule.Initialize()
        {
            if (LoadModule())
            {
                PreInit();
                RegisterServices();
                RegisterViews();
                RegisterMenus();
                SetupModuleInfo();
                PostInit();
            }
        }

        #endregion
    }
}